package com.qinglei.recoup.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.system.domain.BedHealthBreathDailyData;
import com.qinglei.recoup.system.domain.BedHealthDailyStatistics;
import com.qinglei.recoup.system.domain.BedHealthShareParam;
import com.qinglei.recoup.system.pojo.*;
import com.qinglei.recoup.system.service.BedHealthDailyStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author syp
 */
@Api(tags = "健康数据 相关控制器-张-已实现")
@RestController
@RequestMapping("/health")
public class HealthDataController {

    @Resource
    private BedHealthDailyStatisticsService bedHealthDailyStatisticsService;

    @ApiOperation(value = "查询健康数据(二期)")
    @GetMapping
    public CommonRes<IPage<HealthVO>> getHealthPage(@Valid HealthSearchParam param) {
        IPage<HealthVO> healthVOIPage = bedHealthDailyStatisticsService.searchHealthPage(param);
        return CommonRes.ok(healthVOIPage);
    }

    @ApiOperation(value = "查询健康数据(二期)")
    @GetMapping("/list")
    public CommonRes<IPage<HealthVO>> getHealthPageList(HealthDetailParamV1 param) {
        IPage<HealthVO> healthVOIPage = bedHealthDailyStatisticsService.searchHealthPageList(param);
        return CommonRes.ok(healthVOIPage);
    }

    /**
     * 浏览器导出健康数据(二期)
     *
     * @param httpServletResponse
     * @param param
     */
    @ApiOperation(value = "浏览器导出健康数据(二期)")
    @GetMapping("/export")
    public void export(HttpServletResponse httpServletResponse, @Valid HealthSearchParam param) {
        bedHealthDailyStatisticsService.exportHealthData(httpServletResponse, param);
    }

    /**
     * 浏览器导出健康数据(二期)
     *
     * @param httpServletResponse
     * @param param
     */
    @ApiOperation(value = "浏览器导出健康数据(二期)")
    @GetMapping("/exportv1")
    public void exportV1(HttpServletResponse httpServletResponse, @Valid HealthDetailParamV1 param) {
        bedHealthDailyStatisticsService.exportHealthDataV1(httpServletResponse, param);
    }



    @ApiOperation(value = "查询睡眠报告单天(二期)")
    @GetMapping("/oneDay")
    public CommonRes getReportOne(@Valid HealthDetailParam param) {
        Map<String,Object> resultMap = new HashMap<>();
        BedHealthShareParam bedHealthShareParam = new BedHealthShareParam();
        // 设置起止时间条件
        Integer type = param.getType();
        LocalDate today = LocalDate.now();
        LocalDate endDate = today.minusDays(1);
        LocalDate startDate = endDate;
        if (type == 2) {
            endDate = today.minusDays(2);
            startDate = endDate;
        } else if (type == 3 || type == 4) {
            return null;
        }
        bedHealthShareParam.setBedId(param.getBedId());
        bedHealthShareParam.setStartDate(startDate.toString());
        bedHealthShareParam.setEndDate(endDate.toString());
        BedHealthDailyStatistics bedHealthDailyStatistics =   bedHealthDailyStatisticsService.selectBedHealthDailyStatistics(bedHealthShareParam);
        if(bedHealthDailyStatistics!=null && bedHealthDailyStatistics.getResultFlag()==0){
            SleepReportVO sleepReportOne = bedHealthDailyStatisticsService.getSleepReportOne(param);
            resultMap.put("code",200);
            resultMap.put("data",sleepReportOne);
        }else {
            resultMap.put("code",500);
            resultMap.put("data",bedHealthDailyStatistics.getResultFlagStr());
        }
        return CommonRes.ok(resultMap);
    }


    @ApiOperation(value = "查询睡眠报告单天（按天查询）")
    @GetMapping("/oneDayV1")
    public CommonRes getReportOneV1(@Valid HealthDetailParam param) {
        Map<String,Object> resultMap = new HashMap<>();
        BedHealthShareParam bedHealthShareParam = new BedHealthShareParam();
        // 设置起止时间条件
        bedHealthShareParam.setBedId(param.getBedId());
        bedHealthShareParam.setStartDate(param.getDate().toString());
        bedHealthShareParam.setEndDate(param.getDate().toString());
        BedHealthDailyStatistics bedHealthDailyStatistics =   bedHealthDailyStatisticsService.selectBedHealthDailyStatistics(bedHealthShareParam);
        if(bedHealthDailyStatistics!=null && bedHealthDailyStatistics.getResultFlag()==0){
            SleepReportVO sleepReportOne = bedHealthDailyStatisticsService.getSleepReportOneV1(param);
            resultMap.put("code",200);
            resultMap.put("data",sleepReportOne);
        }else {
            resultMap.put("code",500);
            resultMap.put("data",bedHealthDailyStatistics.getResultFlagStr());
        }
        return CommonRes.ok(resultMap);
    }

    @ApiOperation(value = "查询睡眠报告多天(二期)")
    @GetMapping("/moreDay")
    public CommonRes<SleepReportMoreVO> getReportMore(@Valid HealthDetailParam param) {
        SleepReportMoreVO reportMore = bedHealthDailyStatisticsService.getReportMore(param);
        return CommonRes.ok(reportMore);
    }

    @ApiOperation(value = "获取分享健康报告URL")
    @ApiImplicitParam(name = "baseUrl", value = "分享页面URL，调试用，正式环境会配置在服务端", paramType = "query", dataType = "String")
    @GetMapping("/share")
    public CommonRes<String> getShareReportUrl(@Valid HealthDetailParam param, String baseUrl) {
        String shareReportUrl = bedHealthDailyStatisticsService.getShareReportUrl(param, baseUrl);
        return CommonRes.ok(shareReportUrl);
    }


    @ApiOperation(value = "呼吸健康数据，单日")
    @GetMapping("/getHealthReport")
    public CommonRes<BedHealthBreathDailyData> getHealthReport(@Valid HealthDetailParam param) {
        BedHealthBreathDailyData healthReport = bedHealthDailyStatisticsService.getHealthReport(param);
        return CommonRes.ok(healthReport);
    }


    @ApiOperation(value = "呼吸健康数据，单日")
    @GetMapping("/getHealthReportV1")
    public CommonRes<BedHealthBreathDailyData> getHealthReportV1(@Valid HealthDetailParam param) {
        BedHealthBreathDailyData healthReport = bedHealthDailyStatisticsService.getHealthReportV1(param);
        return CommonRes.ok(healthReport);
    }


    @ApiOperation(value = "查询健康数据(院长管理端)")
    @GetMapping("/getHealthPageV1")
    public CommonRes<IPage<HealthVO>> getHealthPageV1(@Valid HealthSearchParam param) {
        IPage<HealthVO> healthVOIPage = bedHealthDailyStatisticsService.searchHealthPageV1(param);
        return CommonRes.ok(healthVOIPage);
    }


    @ApiOperation(value = "获取分享健康报告URL")
    @ApiImplicitParam(name = "baseUrl", value = "分享页面URL，调试用，正式环境会配置在服务端", paramType = "query", dataType = "String")
    @GetMapping("/shareV1")
    public CommonRes<String> getShareReportUrlV1(@Valid HealthDetailParam param, String baseUrl) {
        String shareReportUrl = bedHealthDailyStatisticsService.getShareReportUrlV1(param, baseUrl);
        return CommonRes.ok(shareReportUrl);
    }


    @GetMapping("/getHealthPageV1All")
    public CommonRes getHealthPageV1(Integer bedId){
        Map<String,Object> r = new HashMap();
        LocalDate b = LocalDate.now().minusDays(1);
        List<Map<String,Object>> c = new ArrayList<>();
        BedHealthDailyStatistics k =   bedHealthDailyStatisticsService.selectOldHelthPage(bedId);
        if(k == null ){
            r.put("data",0);
            r.put("bedId",bedId);
            return  CommonRes.ok(r);
        }
        LocalDate l = k.getStatisticsDate();
        int n = 30;
        LocalDate m = b.minusDays(n);
        long o =  l.toEpochDay() - m.toEpochDay();
        if(o>0){
            n = 31-(int) o;
        }
        for(int i=0; i<n;i++){
            LocalDate d = b.minusDays(i);
            BedHealthShareParam e = new BedHealthShareParam();
            e.setBedId(bedId);
            e.setStartDate(d.toString());
            e.setEndDate(d.toString());
            BedHealthDailyStatistics g =   bedHealthDailyStatisticsService.selectBedHealthDailyStatistics(e);
            Map<String,Object> f = new HashMap();
            f.put(d.toString(),g.getResultFlag());
            f.put("reason",g.getResultFlagStr());
            c.add(f);
        }
        r.put("data",c);
        r.put("bedId",bedId);
        return CommonRes.ok(r);
    }

    @GetMapping("/getSleepRuleAndHeartData")
    public CommonRes getSleepRuleAndHeartData(@Valid HealthDetailParamV1 healthDetailParamV1){
        return CommonRes.ok(bedHealthDailyStatisticsService.getHealthReportV2(healthDetailParamV1));
    }

    @GetMapping("/getBreathDataRule")
    public CommonRes getBreathDataRule(@Valid HealthDetailParamV1 healthDetailParamV1){
        return CommonRes.ok(bedHealthDailyStatisticsService.getHealthReportV3(healthDetailParamV1));
    }


    @GetMapping("/getReportInterpretation")
    public CommonRes getReportInterpretation(@Valid HealthDetailParamV1 healthDetailParamV1){
        return CommonRes.ok(bedHealthDailyStatisticsService.getReportInterpretation(healthDetailParamV1));
    }


}
